package wb.is.besti.edu.cd.emergency_responsesys;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.EventLog;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.view.CropImageView;
import com.next.easynavigation.utils.NavigationUtil;
import com.next.easynavigation.view.EasyNavigationBar;

import java.util.ArrayList;
import java.util.List;

import wb.is.besti.edu.cd.emergency_responsesys.pages.BaiDuMapFragment;
import wb.is.besti.edu.cd.emergency_responsesys.pages.FirstPageFragment;
import wb.is.besti.edu.cd.emergency_responsesys.pages.MapPageFragment;
import wb.is.besti.edu.cd.emergency_responsesys.pages.UserFragment;
import wb.is.besti.edu.cd.emergency_responsesys.ui.AddMessageActivity;
import wb.is.besti.edu.cd.emergency_responsesys.ui.PhotoMessageActivity;
import wb.is.besti.edu.cd.emergency_responsesys.utils.Events;
import wb.is.besti.edu.cd.emergency_responsesys.utils.HttpUtil;
import wb.is.besti.edu.cd.emergency_responsesys.view.KickBackAnimator;
import wb.is.besti.edu.cd.emergency_responsesys.widget.GlideImageLoader;

public class HomeActivity extends AppCompatActivity {

    private EasyNavigationBar navigationBar;
    private String[] tableText={"首页","上传","个人"};
    private int[] normalIcon={R.mipmap.index,R.mipmap.add_image,R.mipmap.me};
    private int[] selectIcon={R.mipmap.index1,R.mipmap.add_image,R.mipmap.me1};
    private List<Fragment> fragmentList = new ArrayList<>();
    private int maxImgCount = 8;

    //上传页面

    private int[] menuIconItems={R.mipmap.pic1,R.mipmap.pic2};
    private String[] menuTextItems={"编辑","拍摄"};

    private LinearLayout menuLayout;
    private View cancelImageView;
    private Handler mHandler = new Handler();
    private boolean isFirst = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        initImagePicker();
        navigationBar = findViewById(R.id.navigationBar);
        fragmentList.add(new BaiDuMapFragment());
        fragmentList.add(new UserFragment());
        navigationBar.titleItems(tableText).normalIconItems(normalIcon)
                .selectIconItems(selectIcon).fragmentList(fragmentList)
                .fragmentManager(getSupportFragmentManager()).canScroll(false)
                .anim(null).mode(EasyNavigationBar.MODE_ADD).addAlignBottom(true).onTabClickListener(new EasyNavigationBar.OnTabClickListener() {
            @Override
            public boolean onTabClickEvent(View view, int i) {
                if (i==1){
                    showMenu();
                }
                return false;
            }
        }).build();
        navigationBar.setAddViewLayout(creatOnloadView());

    }
    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(false);                            //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(maxImgCount);              //选中数量限制
        imagePicker.setMultiMode(false);                      //多选
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }
    //弹出窗口
    private View creatOnloadView(){
        ViewGroup viewGroup = (ViewGroup)View.inflate(HomeActivity.this,R.layout.layout_onload_view,null);
        menuLayout = viewGroup.findViewById(R.id.icon_group);
        cancelImageView = viewGroup.findViewById(R.id.cancle_iv);
        cancelImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeAnimation();
            }
        });
        for (int i =0;i<2;i++){
            View itemview = View.inflate(HomeActivity.this,R.layout.item_icon,null);
            ImageView menuImage = itemview.findViewById(R.id.menu_icon_iv);
            TextView menuText = itemview.findViewById(R.id.menu_text_tv);
            menuImage.setImageResource(menuIconItems[i]);
            menuText.setText(menuTextItems[i]);
            if(i==0){
                menuImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AddMessageActivity.startActivity(HomeActivity.this);
                    }
                });
            }
            if(i==1){
                menuImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(HomeActivity.this, "100", Toast.LENGTH_SHORT).show();
                        PhotoMessageActivity.startActivity(HomeActivity.this);
                    }
                });
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            params.weight=1;
            itemview.setLayoutParams(params);
            itemview.setVisibility(View.GONE);
            menuLayout.addView(itemview);
        }
        return viewGroup;
    }

    private void showMenu(){
        startAnimation();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                cancelImageView.animate().rotation(90).setDuration(400);
            }
        });
        //弹出动画
        for (int i=0;i<menuLayout.getChildCount();i++){
            final View child = menuLayout.getChildAt(i);
            child.setVisibility(View.INVISIBLE);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    child.setVisibility(View.VISIBLE);
                    ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child,"translationY",600,0);
                    fadeAnim.setDuration(500);
                    KickBackAnimator kickBackAnimator = new KickBackAnimator();
                    kickBackAnimator.setDuration(500);
                    fadeAnim.setEvaluator(kickBackAnimator);
                    fadeAnim.start();
                }
            },i*50+100);
        }
    }
    /*开始动画*/
    private void startAnimation(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
                        int x = NavigationUtil.getScreenWidth(HomeActivity.this)/2;
                        int y = (int)(NavigationUtil.getScreenHeith(HomeActivity.this)-NavigationUtil.dip2px(HomeActivity.this,25));
                        Animator animator = ViewAnimationUtils.createCircularReveal(navigationBar.getAddViewLayout(),x,y,0,navigationBar.getAddViewLayout().getHeight());
                        animator.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                            }

                            @Override
                            public void onAnimationStart(Animator animation) {
                                navigationBar.getAddViewLayout().setVisibility(View.VISIBLE);
                            }
                        });
                        animator.setDuration(300);
                        animator.start();
                    }
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private void closeAnimation(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                cancelImageView.animate().rotation(0).setDuration(400);
            }
        });
        try {
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
                int x = NavigationUtil.getScreenWidth(this) / 2;
                int y = (NavigationUtil.getScreenHeith(this) - NavigationUtil.dip2px(this, 25));
                Animator animator = ViewAnimationUtils.createCircularReveal(navigationBar.getAddViewLayout(),x,y,navigationBar.getAddViewLayout().getHeight(),0);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        navigationBar.getAddViewLayout().setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                    }
                });
                animator.setDuration(300);
                animator.start();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public EasyNavigationBar getNavigationBar() {
        return navigationBar;
    }

    class NotificationTask extends AsyncTask<Void,Integer,List>{

        private boolean isnull =true;
        private int test =0;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List list) {
            while (isnull){
                if (test<=100){
                    isnull = false;
                }
                test++;
                this.doInBackground();
            }


        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected List doInBackground(Void... voids) {
            List<Events> list = new ArrayList<>();
            list = getEventList();
            return list;
        }
    }

    private List<Events> getEventList(){
        List<Events> eventsList = new ArrayList<>();
        try {
            HttpUtil httpUtil = new HttpUtil();
            eventsList = httpUtil.getMarkerList();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return eventsList;
    }





}
